import { ElMessage } from 'element-plus';
import { useCommentApi } from '/@/api/comment';
import { arrHasData, isEmpty } from '/@/utils';

const { addCommentList, editCommentList, getCommentList } = useCommentApi();

export const useInit = () => {
	const route = useRoute();
	const searchForm = ref({});
	const loading = ref(false);
	const page = ref({
		pageSize: 10,
		current: 1,
		total: 0,
	});
	const tableData = ref([]);
	const init = async () => {
		swapCommentLikeValue();
		loading.value = true;
		let res = await getCommentList({ ...searchForm.value, ...page.value });
		loading.value = false;
		if (res.code !== 200) return;
		const { total, data } = res.data;
		tableData.value = data;
		page.value.total = total;
	};
	onMounted(() => {
		searchForm.value.productId = Number(route.query.id) || undefined;
		init();
	});
	function swapCommentLikeValue() {
		const { commentLikeStart, commentLikeEnd } = searchForm.value;
		if (!commentLikeStart || !commentLikeEnd || commentLikeStart < commentLikeEnd) return;
		searchForm.value.commentLikeStart = commentLikeEnd;
		searchForm.value.commentLikeEnd = commentLikeStart;
	}
	return {
		page,
		searchForm,
		loading,
		tableData,
		init,
		async changeStatus(row) {
			let { id, status } = row;
			const _status = status === 1 ? 0 : 1;
			let res = await editCommentList({ id, status: _status });
			if (res.code !== 200) return;
			ElMessage.success('更改状态成功！');
			init();
		},
	};
};
export const useAdd = () => {
	const isAdd = ref(false);
	const addDialogVisible = ref(false);
	const ruleForm = ref({
		questionList: [],
	});

	function validator(ruleForm) {
		if (!ruleForm.tempName) {
			ElMessage.warning('请填写模板名称');
			return false;
		}
		let isValid = true;
		ruleForm.questionList.some((item1, index) => {
			//第一层：问题与评价方式
			if (isEmpty(item1.question)) {
				ElMessage.warning(`请输入问题(问题${index + 1})`);
				isValid = false;
			} else if (!item1.commentMethod) {
				ElMessage.warning(`请选择评价方式(问题${index + 1})`);
				isValid = false;
			} else {
				//第二层：单选与数值
				if (item1.commentMethod === 1) {
					if (!arrHasData(item1.optionList)) {
						ElMessage.warning(`请添加至少一个选项(问题${index + 1})`);
						isValid = false;
					} else {
						item1.optionList.some((item2, index2) => {
							if (isEmpty(item2)) {
								ElMessage.warning(`请输入选项(问题${index + 1}-选项${index2 + 1})`);
								isValid = false;
								return true;
							}
						});
					}
				} else if (item1.commentMethod === 2 && !item1.num) {
					ElMessage.warning(`请输入数值(问题${index + 1})`);
					isValid = false;
				}
			}
			return !isValid;
		});
		return isValid;
	}
	return {
		isAdd,
		addDialogVisible,
		ruleForm,
		title: computed(() => (isAdd.value ? '新增评价' : '查看评价')),
		toAdd() {
			isAdd.value = true;
			addDialogVisible.value = true;
		},
		toEdit(row) {
			addDialogVisible.value = true;
			isAdd.value = false;
			ruleForm.value = row;
		},
		// 新增问题
		addQuestion() {
			ruleForm.value.questionList.push({});
		},
		//删除问题
		delQuestion(index) {
			ruleForm.value.questionList.splice(index, 1);
		},
		//新增单选项
		addOption(index) {
			if (!ruleForm.value.questionList[index].optionList) ruleForm.value.questionList[index].optionList = [];
			ruleForm.value.questionList[index].optionList.push('');
		},
		//删除选项
		delOption(index1, index2) {
			ruleForm.value.questionList[index1].optionList.splice(index2, 1);
		},
		submit() {
			if (!validator(ruleForm.value)) return;
		},
	};
};

export const commentMethodOptions = [
	{
		value: 1,
		label: '单选',
	},
	{
		value: 2,
		label: '数值',
	},
	{
		value: 3,
		label: '填空',
	},
	{
		value: 4,
		label: '上传图片',
	},
];
